package org.lhg.activiti.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.lhg.activiti.model.PageBean;
import org.lhg.activiti.model.StudentLeave;
import org.lhg.activiti.model.User;
import org.lhg.activiti.service.LeaveService;
import org.lhg.activiti.util.CommonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/leave")
public class LeaveController {


    @Autowired
    private LeaveService leaveService;


    /**
     * 请假表分页
     * @param page
     * @param limit
     * @param userId
     * @return
     */
    @RequestMapping(value = "/listLeave",method = RequestMethod.GET)
    public CommonResult<JSONObject> listLeave(@RequestParam(value="page",defaultValue="0")String page, @RequestParam(value="limit",defaultValue="0")String limit, String userId){
        PageBean pageBean=new PageBean(Integer.parseInt(page),Integer.parseInt(limit));
        Map<String,Object> map=new HashMap<String,Object>();
        map.put("userId",userId); // 用户名
        map.put("start", pageBean.getStart());
        map.put("size", pageBean.getPageSize());
        List<StudentLeave> leaveList=leaveService.listLeave(map);
        Long total=leaveService.getTotal(map);

        JSONObject result=new JSONObject();
        JSONArray jsonArray=JSONArray.parseArray(JSON.toJSONString(leaveList));
        result.put("rows", jsonArray);
        result.put("total", total);
        return CommonResult.success(result);
    }

    /**
     * 新增请假单
     * @param leave
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/addLeave",method = RequestMethod.POST)
    public CommonResult<JSONObject> addLeave(@RequestBody StudentLeave leave) throws Exception {
        int resultTotal=0;
        leave.setLeaveDate(new Date());
        User user=new User();
        user.setId(leave.getUserId());
        leave.setUser(user);
        resultTotal=leaveService.addLeave(leave);
        JSONObject result=new JSONObject();
        if(resultTotal>0){
            result.put("success", true);
        }else{
            result.put("success", false);
        }
        return CommonResult.success(result);
    }

    /**
     *  通过流程实例ID获取请假单
     * @param map
     * @return
     * @throws Exception
     */
    @RequestMapping(value="/leave_by_processInstanceId",method = RequestMethod.POST)
    public CommonResult<JSONObject> leave_by_processInstanceId(@RequestBody Map<String,Object> map)throws Exception{
        String processInstanceId=map.get("processInstanceId").toString();
        StudentLeave leave=leaveService.leave_by_processInstanceId(processInstanceId);
        JSONObject result=new JSONObject();
        result.put("leave", JSONObject.parse(JSON.toJSONString(leave)));
        return CommonResult.success(result);
    }

    /**
     * 完成审批
     * @param map
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/finshApply",method = RequestMethod.POST)
    public CommonResult<JSONObject> finshApply(@RequestBody Map<String,Object> map)throws Exception{
        String processInstanceId=map.get("processInstanceId").toString();
        StudentLeave leave=leaveService.leave_by_processInstanceId(processInstanceId);

        String state=map.get("state").toString();
        if(state.equals("批准")){
            leave.setState("审核通过");
        }else{
            leave.setState("审核未通过");
        }
        leaveService.update(leave); // 修改请假单状态
        JSONObject result=new JSONObject();
        result.put("success", true);
        return CommonResult.success(result);
    }

}
